package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.operator.pojo.TbSpecification;
import com.dongdongshop.operator.pojo.TbSpecificationOption;
import com.dongdongshop.operator.pojo.TbSpecificationOptionExample;
import com.dongdongshop.operator.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

@Service
@Component
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;
    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public List<TbSpecification> getSpecification(String specName) {
        List<TbSpecification> tbSpecifications = specificationMapper.getSpecification(specName);
        return tbSpecifications;
    }

    @Override
    public List<TbSpecification> getByid(Long id) {
        List<TbSpecification> list = specificationMapper.getByid(id);
        return list;
    }

    @Override
    @Transactional
    public void updateSpecification(TbSpecification specification,String[] optionNameList, Integer[] orderList) {
        //修改主表
        specificationMapper.updateByPrimaryKey(specification);
        //修改次表，先删除后增加
        Long id = specification.getId();
        //根据主表id删除次表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
        //批量添加
        List<TbSpecificationOption> specificationOptionList = new ArrayList<>();


        for (Integer i = 0; i < optionNameList.length; i++) {

            String s = optionNameList[i];

            TbSpecificationOption specificationOption = new TbSpecificationOption();
            specificationOption.setOptionName(s);
            specificationOption.setSpecId(id);

            for (Integer j = 0; j < orderList.length; j++) {
                Integer order = orderList[j];
                /* 二者下標相等 才添加 */
                if (j.equals(i)) {

                    specificationOption.setOrders(order);
                }
            }
            specificationOptionList.add(specificationOption);
        }
        specificationOptionMapper.inserts(specificationOptionList);
    }

    @Override
    @Transactional
    public void deleteSpecificationByIds(Long[] ids) {
        //删除主表
        specificationMapper.deletesSpecification(ids);
        //删除从表
        specificationOptionMapper.deleteSpecificationOption(ids);
    }

    @Override
    @Transactional
    public void addSpecification(TbSpecification specification, String[] optionNameList, Integer[] orderList) {
        //增加主表并返回id
        specificationMapper.addSpecification(specification);
        //返回的id
        Long id = specification.getId();
        List<TbSpecificationOption> specificationOptionList = new ArrayList<>();

        for (Integer i = 0; i < optionNameList.length; i++) {

            String s = optionNameList[i];

            TbSpecificationOption specificationOption = new TbSpecificationOption();
            specificationOption.setOptionName(s);
            specificationOption.setSpecId(id);

            for (Integer j = 0; j < orderList.length; j++) {
                Integer order = orderList[j];
                /* 二者下標相等 才添加 */
                if (j.equals(i)) {

                    specificationOption.setOrders(order);
                }
            }
            specificationOptionList.add(specificationOption);
        }
        specificationOptionMapper.inserts(specificationOptionList);
    }

    @Override
    public List<TbSpecificationOption> getSpecificationOptionBySpecIds(Long specId) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(specId);
        List<TbSpecificationOption> tbSpecificationOptions = specificationOptionMapper.selectByExample(example);
        return tbSpecificationOptions;
    }
}
